package com.yuanfengPlus.expenseitem.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuanfengPlus.expenseitem.dao.IExpenseItemMapper;
import com.yuanfengPlus.expenseitem.model.ExpenseItemInfo;
import com.yuanfengPlus.expenseitem.service.IExpenseItemService;
import com.yuanfengPlus.utils.PageResult;
import com.yuanfengPlus.utils.Result;
import com.yuanfengPlus.utils.SnowflakeIdWorker;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class ExpenseItemServiceImpl implements IExpenseItemService{
    @Resource
    private IExpenseItemMapper ieim;
    private static final Logger logger = LoggerFactory.getLogger(IExpenseItemService.class);
    // 获取东八区时区的ZoneId
    ZoneId zoneId = ZoneId.of("Asia/Shanghai");

    // 获取当前时间的Instant
    Instant nowInstant = Instant.now();

    // 将Instant转换为东八区的ZonedDateTime
    ZonedDateTime zonedDateTime = nowInstant.atZone(zoneId);

    // 将ZonedDateTime转换为Timestamp
    Timestamp timestamp = Timestamp.from(zonedDateTime.toInstant());
    @Override
    public Result<String> insert(ExpenseItemInfo expenseItemInfo) {
        if (expenseItemInfo == null)
            return Result.failure(500,"支出项目信息不能为空");
        if (expenseItemInfo.getExpenses_name() == null)
            return Result.failure(500,"支出项目名称不能为空");
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(3);
        expenseItemInfo.setExpenses_ID(snowflakeIdWorker.nextId());
        expenseItemInfo.setExpenses_state(0);
        expenseItemInfo.setDeleted(0);
        expenseItemInfo.setExpenses_operate_time(timestamp);
        expenseItemInfo.setExpenses_review_time(timestamp);
        Integer insert = ieim.insert(expenseItemInfo);
        if (insert <= 0)
            return Result.failure(500,"添加失败");
        return Result.success("添加成功");
    }

    @Override
    public Result<String> update(ExpenseItemInfo expenseItemInfo) {
        try {
            Integer updateCount = ieim.update(expenseItemInfo);
            if (updateCount > 0) {
                logger.info("Expense item updated successfully.");
                return Result.success("修改成功");
            } else {
                logger.warn("Failed to update expense item.");
                return Result.failure(500, "修改失败");
            }
        } catch (Exception e) {
            logger.error("Error occurred while updating expense item.", e);
            return Result.failure(500, "系统错误: " + e.getMessage());
        }
    }

    @Override
    public PageResult select(Map<String, Object> params) {
        // 检查并设置默认值
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);

        // 确保 pageNum 和 pageSize 不为 null
        if (pageNum == null || pageSize == null) {
            throw new IllegalArgumentException("pageNum 和 pageSize 不能为 null");
        }

        PageHelper.startPage(pageNum, pageSize);

        // 确保 ilocm 不为 null

        if (ieim == null) {
            throw new IllegalStateException("ieim 未正确初始化");
        }

        Page<ExpenseItemInfo> page = ieim.select(params);

        // 确保 page 不为 null
        if (page == null) {
            throw new IllegalStateException("查询结果为空");
        }

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Result<String> delete(String expense_ID) {
        ExpenseItemInfo expenseItemInfo = ieim.selectById(expense_ID);
        if (expenseItemInfo == null)
            return Result.failure(500, "未找到对应的记录");
        expenseItemInfo.setDeleted(1);
        Integer delete = ieim.delete(expense_ID);
        if (delete <= 0)
            return Result.failure(500, "删除失败");
        return Result.success("删除成功");
    }


    @Override
    public Result<List<ExpenseItemInfo>> selectDusbtin() {
        List<ExpenseItemInfo> expenseItemInfos = ieim.selectDusbtin();
        if (expenseItemInfos == null && expenseItemInfos.isEmpty())
            return Result.failure(500,"垃圾箱是空的");
        return Result.success(expenseItemInfos);
    }


}
